package blog.controller;

import blog.entity.BatchResult;
import blog.entity.QueryJson;
import blog.entity.ResultJson;
import blog.pojo.Chapter;
import blog.pojo.ChapterCategory;
import blog.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private ChapterCategoryService chapterCategoryService;

    @Autowired
    private ChapterRecordService chapterRecordService;

    @Autowired
    private ChapterLabelService chapterLabelService;

    @Autowired
    private ChapterChapterLabelService chapterChapterLabelService;

    @Autowired
    private ChapterChapterCategoryService chapterChapterCategoryService;

    @Autowired
    private ChapterCommentService chapterCommentService;

    @RequestMapping("edit")
    public ResultJson edit(@RequestBody QueryJson<Chapter> queryJson) throws Exception{
        if(queryJson.getEntity() == null) {
            return ResultJson.ARGUMENT_NULL;
        }
        Chapter chapter = queryJson.getEntity();
        Integer chapterId = queryJson.getEntity().getId();
        if(chapterId != null){
            chapterService.update(chapter);
        } else {
            chapterId = chapterService.insertGetId(chapter);
        }
        chapterChapterCategoryService.edit(chapterId, chapter.getCategoryIdList());
        chapterChapterLabelService.edit(chapterId, chapter.getLabelIdList());

        return ResultJson.SUCCESS;
    }

    @RequestMapping("update")
    public ResultJson update(@RequestBody QueryJson<Chapter> queryJson) throws Exception{
        if(queryJson.getEntity() == null) {
            return ResultJson.ARGUMENT_NULL;
        }
        chapterService.update(queryJson.getEntity());

        return ResultJson.SUCCESS;
    }

    @RequestMapping("/updateManyByIds")
    public ResultJson updateManyByIds(@RequestBody QueryJson<Chapter> queryJson) throws Exception{
        if(queryJson.getEntity() == null || queryJson.getIds() == null){
            return ResultJson.ARGUMENT_NULL;
        }
        int success = 0, fail = 0;
        for(Integer id : queryJson.getIds()){
            Chapter chapter = queryJson.getEntity().setId(id);
            if (chapterService.update(chapter)) {
                success++;
            } else {
                fail++;
            }
        }
        return ResultJson.isSuccess(BatchResult.SET(success,fail));
    }


    @RequestMapping("/findPage")
    public ResultJson findPage(@RequestBody QueryJson<Chapter> queryJson) throws Exception{
        if(queryJson.getPage() == null) return ResultJson.ARGUMENT_NULL;

        Page<Chapter> page = chapterService.getPage(queryJson.getPage(), queryJson.getEntity(), queryJson.getKeywords());
        for(Chapter chapter: page.getRecords()){
            chapter.setRecord(chapterRecordService.getByChapterId(chapter.getId()));
            chapter.setCategoryList(chapterCategoryService.getListByChapterId(chapter.getId()));
            chapter.setLabelList(chapterLabelService.getListByChapterId(chapter.getId()));
        }

        return ResultJson.isSuccess(page);
    }

    @RequestMapping("/find")
    public ResultJson find(@RequestBody QueryJson<Chapter> queryJson) throws Exception{
        Integer id = queryJson.getEntity().getId();
        if(id == null) return ResultJson.ARGUMENT_NULL;
        Chapter chapter = chapterService.getInfo(id);
        chapter.setCategoryList(chapterCategoryService.getListByChapterId(id));
        chapter.setLabelList(chapterLabelService.getListByChapterId(id));
        return ResultJson.isSuccess(chapter);
    }
}
